Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
How to list "new" member of an JavaScript object that is not instance/static member from its base or inherited class?

I'm not sure how to define this new member thing, please check demo below, what I want is ListNewMemberNames:

The code (demo.html) is assuming running in a browser

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Var1 Test</title>
</head>
<body>
    <script>
        var1 = {value:7}  /* same as window.var1 = {value:7}*/

        ListMemberNames(window) 
        /* returns: ["__defineGetter__", ... "addEventListener", "alert", ... "var1"] */

        ListNewMemberNames(window)
        /* returns: ["var1"] */
        
        function ListMemberNames(obj){ /* tricks with .hasOwnProperty() or something*/ }
        function ListNewMemberNames(obj){ /* ? */ }
    </script>
</body>
</html>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Generally, you can't analyze the current (or outer) lexical environment (the mapping of variable names to their values) dynamically in JavaScript. This is only possible on the top level due to the fact that top-level variables declared with var (or with nothing at all) get assigned to the window - check the properties that exist on the window at the beginning of pageload, put it into an array, then look at the properties that exist on it later, and find the difference.

// always run this first
const properties = Object.getOwnPropertyNames(window);

// then your code does something
var1 = {value:7}

// and it is detectable by doing:
const newPropertiesOnWindow = Object.getOwnPropertyNames(window)
  .filter(prop => !properties.includes(prop));
console.log(newPropertiesOnWindow);

It's possible to do, barely - but I can't think of any good reason to want to do so.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda